﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.DemandPlanning;
using THE_WIND_RISES.Common.Domans.Produce.ManifestInfos;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.MaterialManagement.Add
{
    public class RowMaterialPlanningCreateHandler : IRequestHandler<RowMaterialPlanningCreateCommon, Result>
    {
        private readonly IBaseRepository<MaterialPlanningRow> MaterialPlanningRowRepository;
        public IMapper mapper { get; set; }
        public RowMaterialPlanningCreateHandler(IBaseRepository<MaterialPlanningRow> materialPlanningRowRepository, IMapper mapper)
        {
            MaterialPlanningRowRepository = materialPlanningRowRepository;
            this.mapper = mapper;
        }
        public async Task<Result> Handle(RowMaterialPlanningCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Added };
            var AddData = mapper.Map<MaterialPlanningRow>(request);
            string sql = "insert into MaterialPlanningRow values(@PlanningRow,@ChooseId,@AttributeRowId,@UnitName,@Spec,@PlanningNum,@PlanningStartDate,@PlanningEndDate,@Markment,@Handle,@HandleContext,@HandleTime,@Pid,@IsDelete);";
            var AddNum = MaterialPlanningRowRepository.Add(sql, new
            {
                AddData.PlanningRow,
                AddData.ChooseId,
                AddData.AttributeRowId,
                AddData.UnitName,
                AddData.Spec,
                AddData.PlanningNum,
                AddData.PlanningStartDate,
                request.PlanningEndDate,
                AddData.Markment,
                AddData.Handle,
                AddData.HandleContext,
                AddData.HandleTime,
                AddData.Pid,
                AddData.IsDelete,
            });
            if (AddNum > 0)
            {
                result.Code = (int)CommonErrorCodes.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
